<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改手机号</title>
    <meta name="viewport" content=" initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <link href="{$Think.config.RESOURCEURL}wx_assets/iconfont/iconfont.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <link type="text/css" rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/zepto.alert.css" />
    <link href="{$Think.config.RESOURCEURL}wx_assets/personCenter/css/index.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <script src="{$Think.config.RESOURCEURL}wx_assets/personCenter/js/mediaQuery.min.js{$Think.config.RESOURCE_VER}"></script>
    <script type='text/javascript' src='{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js'></script>
    <script src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js" type="text/javascript"></script>
    <script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}"></script>
</head>
<body id="change-phone">
  <ul class="form">
    <li>
      <i class="iconfont">&#xe627;</i>
      <input type="number" placeholder="请输入手机号" class="phone" value="{$phone}"/>
    </li>
    <li>
      <i class="iconfont">&#xe64a;</i>
      <input type="number" placeholder="请输入验证码" class="code"/>
      <span class="sms-btn">获取验证码</span>
    </li>
  </ul>
  <div class="save-btn">保存</div>
<script>
  function Page() {};

  $.extend(Page.prototype, {
      init: function () {
          this.bindEvents();
          console.log($.util.getUa())
      },

      // 绑定事件
      bindEvents: function() {
          $('.sms-btn').click($.proxy(this.handleSendSmsBtnClick, this));
          $('.save-btn').click($.proxy(this.handleSaveBtnClick, this));
      },

      // 点击发送验证码按钮
      handleSendSmsBtnClick: function() {
        if ($('.sms-btn').hasClass('disabled')) return;
        var phone = $('.phone').val();
        if (!phone) {
          $.util.alert("请输入手机号码！");
          return false;
        }
        if(!(/^1[34578]\d{9}$/.test(phone))){
          $.util.alert("手机号码有误，请重填");
          return false;
        }
        var params = {
          phone: phone,
          type: 1
        }
        $.ajax({
            type: "get",
            url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
            dataType: 'json',
            data: {
              module: 'personCenter',
              method: 'verifyPhone',
              params: JSON.stringify(params)
            },
            success: $.proxy(this.handleGteSmsSucc, this)
        });
      },

      // 获取短信成功后执行
      handleGteSmsSucc: function(res) {
        if (+res.code === 0) {
          $('.sms-btn').addClass('disabled');
          var second = 60;
          $('.sms-btn').text('60s后重新获取')
          var timer = setInterval(function(){
            $('.sms-btn').text(--second + 's后重新获取')
            if (second <= 0) {
              clearInterval(timer);
              $('.sms-btn').removeClass('disabled');
              $('.sms-btn').text('获取验证码');
            }
          }, 1000)
        } else {
          $.util.alert(res.msg);
        }
      },

      // 点击保存按钮
      handleSaveBtnClick: function() {
        var phone = $('.phone').val();
        var code = $('.code').val();
        if (!phone) {
          $.util.alert("请输入手机号码！");
          return false;
        }
        if(!(/^1[34578]\d{9}$/.test(phone))){
          $.util.alert("手机号码有误，请重填");
          return false;
        }
        if (!code) {
          $.util.alert("请输入验证码！");
          return false;
        }
        var params = {
          phone: phone,
          code: code
        }
        $.ajax({
            type: "get",
            url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
            dataType: 'json',
            data: {
               module: 'personCenter',
               method: 'changePhone',
               params: JSON.stringify(params)
            },
            success: $.proxy(this.handleSaveSucc, this),
            error: $.proxy(this.handleSaveError, this)
        });
      },

      // 保存成功
      handleSaveSucc: function(res) {
        if (+res.code === 0) {
          $.util.alert(res.msg);
          history.back();
          sessionStorage.setItem('refresh', 'true');
        } else {
          $.util.alert(res.msg);
        }
      },

      // ajax请求出错
      handleSaveError: function(err) {
        $.util.alert(err);
      }
  }) ;

  var page = new Page();
  page.init();
</script>
</body>
</html>
